class Solution(object):
    def lastStoneWeightII(self, stones):
        if len(stones) == 1:
            return stones[0]
        t = sum(stones)
        target = t // 2
        now = set()
        for s in stones:
            next_set = set()
            if s <= target:
                next_set.add(s)
                for node in now:
                    next_set.add(node)
                    if node + s <= target:
                        next_set.add(node + s)
                now = next_set
        return t - 2 * max(now)



